package com.dmc.hrl.domain.entity;

import com.baomidou.mybatisplus.core.metadata.IPage;

/**
 * 对于返回结果操作工具类
 * 
 * 约定：RestController的返回结果必须声明泛型-错误方式以运行时异常抛出让RestControllerAdvice或者切面捕捉处理
 * 
 * @author Peng
 *
 */
public class ResultEntity {

	public static final int SUCCESS = 2000; // 操作成功
	public static final int FAILED = 5000; // 操作失败
	public static final int VALIDATE_FAILED = 4004; // 参数校验失败
	public static final int UNAUTHORIZED = 4001; // 未认证
	public static final int FORBIDDEN = 4003; // 未授权

	private static final String SIMPLE_SUCCESS_MESSAGE = "请求成功";
	private static final String SIMPLE_FAIL_MESSAGE = "请求失败";
	private static final String SIMPLE_VALIDATE_FAILED_MESSAGE = "参数验证失败";
	private static final String SIMPLE_UNAUTHORIZED_MESSAGE = "未登录";
	private static final String SIMPLE_FORBIDDEN_MESSAGE = "没有权限";

	// 私有化构造器
	private ResultEntity() {
	}

	/**
	 * 成功结果
	 * 
	 */
	public static Result<?> ok() {
		return ok(null, null);
	}

	// 存在歧义
	public static Result<?> ok(String message) {
		return ok(message, null);
	}

	public static <T> Result<T> ok(T data) {
		return ok(null, data);
	}

	public static <T> Result<T> ok(String message, T data) {
		if (message == null || message.equals("")) {
			message = SIMPLE_SUCCESS_MESSAGE;
		}
		return new Result<T>(true, SUCCESS, message, data);
	}

	/**
	 * 失败结果
	 */
	public static Result<?> failed() {
		return failed(null, null);
	}

	// 存在歧义
	public static Result<?> failed(String message) {
		return failed(message, null);
	}

	public static <T> Result<T> failed(T data) {
		return failed(null, data);
	}

	public static <T> Result<T> failed(String message, T data) {
		if (message == null || message.equals("")) {
			message = SIMPLE_FAIL_MESSAGE;
		}
		return new Result<T>(false, FAILED, message, data);
	}

	/**
	 * 参数验证失败使用
	 *
	 */
	public static Result<?> validateFailed() {
		return validateFailed(null);
	}

	public static Result<?> validateFailed(String message) {
		if (message == null || message.equals("")) {
			message = SIMPLE_VALIDATE_FAILED_MESSAGE;
		}
		return new Result<>(false, VALIDATE_FAILED, message, null);
	}

	/**
	 * 未登录时使用
	 *
	 */
	public static Result<?> unauthorized() {
		return unauthorized(null);
	}

	public static Result<?> unauthorized(String message) {
		if (message == null || message.equals("")) {
			message = SIMPLE_UNAUTHORIZED_MESSAGE;
		}
		return new Result<>(false, UNAUTHORIZED, message, null);
	}

	/**
	 * 未授权时使用
	 *
	 */
	public static Result<?> forbidden() {
		return forbidden(null);
	}

	public static Result<?> forbidden(String message) {
		if (message == null || message.equals("")) {
			message = SIMPLE_FORBIDDEN_MESSAGE;
		}
		return new Result<>(false, FORBIDDEN, message, null);
	}

	/**
	 * 返回分页成功数据(Result结果）
	 */
	public static <T> Result<PageResult<T>> okPage(PageResult<T> result) {
		return ok(null, okPageX(result));
	}

	/**
	 * 返回分页成功数据(Result结果）
	 */
	public static <T> Result<IPage<T>> okIPage(String message , IPage<T> result) {
		return ok(message, result);
	}

	/**
	 * 返回分页成功数据（直接返回PageResult对象）(不推荐使用）
	 */
	@Deprecated
	public static <T> PageResult<T> okPageX(PageResult<T> result) {
		return result;
	}



}
